package com.invengo.library_rfid.analyze.impl;

import com.invengo.library_rfid.analyze.EPCStrategy;
import com.invengo.library_rfid.analyze._EpcRuleTag;
import com.invengo.library_rfid.analyze.utils.hf.HFDataFormat;

import java.io.Serializable;

import me.goldze.mvvmhabit.utils.KLog;

public class FuNingEPCStrategyImpl implements EPCStrategy , Serializable {
    @Override
    public _EpcRuleTag getRule() {
        return _EpcRuleTag.RULE_FuNing;
    }

    @Override
    public String analyzeShelf(String epc) {
        if (epc.startsWith("FF")) {
            String substring = epc.substring(2, 16);
            KLog.e(substring+"----");
            return substring;
        }
        String udata = reverseBitsIn8Groups(epc.substring(0, 16));
        String substring = udata.substring(2, udata.length());
        KLog.e(substring+"----111");
        return substring;
    }

    public static String reverseBitsIn8Groups(String code) {
        StringBuilder resultBuilder = new StringBuilder();
        // 按照每8个字符分割字符串
        for (int i = 0; i < code.length(); i += 8) {
            // 获取当前8位字符子串
            String group = code.substring(i, Math.min(i + 8, code.length()));
            System.out.println("group = " + group);
            // 反转该子串
            char[] chars = group.toCharArray();
            char[] tt = new char[chars.length];
            for (int n = 0; n < chars.length; n += 2) {
                tt[chars.length - n - 2] = chars[n];
                tt[chars.length - n - 1] = chars[n + 1];
            }
            resultBuilder.append(new String(tt));
        }
        return resultBuilder.toString();
    }

    @Override
    public String analyzeBook(String epc) {
        return HFDataFormat.Deserialise(epc);
    }
}
